package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.SysPrivilege;
import com.bjsxt.mapper.SysPrivilegeMapper;
import com.bjsxt.service.SysPrivilegeService;
import org.springframework.util.CollectionUtils;

@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{

    @Autowired
private SysPrivilegeMapper sysPrivilegeMapper;
    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId, Long roleId) {
        //获得该菜单下所有的权限
       List<SysPrivilege> sysPrivilegeList = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId,menuId));
       if (CollectionUtils.isEmpty(sysPrivilegeList))
       {
           return Collections.emptyList();
       }
       /*获得角色所拥有的权限*/
        Set<Long> currentRoleSysPrivilegeIds = sysPrivilegeMapper.getPrivilegesByRoleId(roleId);
       for(SysPrivilege sysPrivilege :sysPrivilegeList)
       {

           if(currentRoleSysPrivilegeIds.contains(sysPrivilege.getId()))
           {
               sysPrivilege.setOwn(1);
           }
       }



        return sysPrivilegeList;
    }
}
